import './index.scss';
import tpl from './index.tpl';
import CheckBox from '../check_box/index';

const checkBox = new CheckBox();

export default () => {
  return {
    name: 'purchase',
    tpl(price) {
      let html = '';
      html = tpl().replace(/{{(.*?)}}/g, (node, key) => {
        return {
          price: price,
          check_box: checkBox.tpl(0, 'mainCheck', undefined)
        }[key];
      });

      let oBox = document.createElement('div');
      oBox.className = 'purchase-box';
      oBox.innerHTML = html;

      return oBox;
    },

    selectAll(ev, cartInfos) {
      let e = ev || window.Event,
        tar = e.target || e.srcElement,
        className = tar.className;

      const oSubChecks = Array.from(document.getElementsByClassName('J_subCheck')),
        oMainCheck = document.getElementsByClassName('J_mainCheck')[0];

      if(className === 'check-input J_mainCheck'){
        oSubChecks.forEach((elem, index) => {
          console.log(index);
          elem.checked = oMainCheck.checked;
          cartInfos.list[index].checked = oMainCheck.checked;
        });
      }
    }
  }
}